package com.eleventwelve.iris.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.opentides.bean.FileInfo;
import org.opentides.service.FileInfoService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.eleventwelve.iris.core.bean.JSONKeyValue;
import com.eleventwelve.iris.entity.Patient;
import com.eleventwelve.iris.service.PatientService;

public class AjaxRequestController extends MultiActionController {
	private String successView;
	
	private FileInfoService fileInfoService;
	private PatientService patientService;

	public ModelAndView removePatientFile(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> model = new HashMap<String, Object>();
		List<JSONKeyValue> results = new ArrayList<JSONKeyValue>();

		String patientId = request.getParameter("patientId");
		String fileId = request.getParameter("fileId");
		
		Patient patient = patientService.load(patientId);
		FileInfo file = fileInfoService.load(fileId);
		
		patient.getFiles().remove(file);
		
		patientService.save(patient);
		fileInfoService.delete(fileId);
		
		results.add(new JSONKeyValue(0L, "true"));

		model.put("results", results);
		return new ModelAndView(successView, model);
	}
	
	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}
	
	public void setFileInfoService(FileInfoService fileInfoService) {
		this.fileInfoService = fileInfoService;
	}
	
	public void setSuccessView(String successView) {
		this.successView = successView;
	}
}
